var mysql = require('mysql')
// var db = mysql.createConnection({
//   host: 'localhost',
//   user: 'root',
//   password: '123456',
//   database: 'learn'
// })
// db.connect(function(err) {
//   if (err) {
//     console.error('error connecting: ' + err.stack);
//     return
//   }
//
//   console.log('connected as id ' + db.threadId);
//   db.query('select * from table_name', function(error, results, fields) {
//     if (error) throw error;
//     console.log('The solution is: ', results);
//     // console.log(fields);
  // });
// })
// var pool  = mysql.createPool({
//     host     : 'localhost',
//     user     : 'root',
//     password : '123456',
//     database:'learn'
// })
//
// pool.getConnection(function(err, connection) {
//   if(err) {
//      console.error('error connecting: ' + err.stack);
//      return
//   }
//     pool.query('select * from table_name', function (error, results, fields) {
//       if (error) throw error;
//       console.log('The solution is: ', results);
//       // console.log(fields);
//     })
// })
var Sequelize=require('sequelize');
const sequelize = new Sequelize('learn', 'root', '123456', {
  host: 'localhost',
  dialect: 'mysql',
  pool: {
    max: 5,
    min: 0,
    acquire: 30000,
    idle: 10000
  }
});
sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  })
  var User = sequelize.define('user', {
  username: Sequelize.STRING,
  password: Sequelize.STRING,
  mail:Sequelize.STRING
});
sequelize.sync().then(function() {
  //创建表并创建我们第一个用户
  return User.create({
      username:'wt',
      password:'456789',
      mail:'wt@qq.com'
  });
}).then(function(data){
  console.log(data);
}).catch(function(err){
  console.log('inserted fail');
  console.log(err.message);
})
